using Axiom.Core;
using Axiom.Math;
using Utility.Input;
using System.Windows.Forms;

namespace Utility.Navigation  {
	public class CylindricalNavigationManager : NavigationManager {



        public CylindricalNavigationManager(QuadtreeSceneManager q, Camera c, InputReader i, Control u)
            : base(q, c, i, u)
        {
            camera.Position = Vector3.UnitZ * Planet.OuterOrbitRadius;
            camera.LookAt(Vector3.Zero);
            camera.Near = 100f;
            camera.Far = camera.Position.Length * 2.5f;
		}

        public override void Navigate(FrameEventArgs e)
        {
            // TODO : input.Capture Render.onFrameStart ta olmali ama orada calismiyor. Neden anla, duzelt?
            input.Capture();

            //find the initial azimuth and elevation angles of the current position
            double r = camera.Position.Length;
            double gamma = System.Math.Atan2(camera.Position.z, camera.Position.x);
            double theta = System.Math.Acos(camera.Position.y / r);
                 

            float thetaOffset = 0, gammaOffset = 0;
            if (input.IsKeyPressed(KeyCodes.W))
            {
                thetaOffset += 0.05f;
            }
            if (input.IsKeyPressed(KeyCodes.S))
            {
                thetaOffset -= 0.05f;
            }
            if (input.IsKeyPressed(KeyCodes.D))
            {
                gammaOffset += 0.05f;
            }
            if (input.IsKeyPressed(KeyCodes.A))
            {
                gammaOffset -= 0.05f;
            }

            theta += thetaOffset;
            gamma += gammaOffset;

            camera.Position = new Vector3((float)(r * System.Math.Cos(gamma) * System.Math.Sin(theta)),
                (float)(r * System.Math.Cos(theta)),
                (float)(r * System.Math.Sin(gamma) * System.Math.Sin(theta)));
            camera.LookAt(Vector3.Zero);
            if (input.RelativeMouseZ > 0)
            {
                camera.Position *= 0.95f;
            }

            if (input.RelativeMouseZ < 0)
            {
                camera.Position *= 1.05f;
            }

            if (camera.Position.Length > (Planet.OuterOrbitRadius * VectorProjection.PARAMETER_UNIT_FACTOR))
            {
                camera.Position = (camera.Position / camera.Position.Length) * (Planet.OuterOrbitRadius * VectorProjection.PARAMETER_UNIT_FACTOR);
            }

            if (camera.Position.Length < (Planet.InnerOrbitRadius * VectorProjection.PARAMETER_UNIT_FACTOR))
            {
                camera.Position = (camera.Position / camera.Position.Length) * (Planet.InnerOrbitRadius * VectorProjection.PARAMETER_UNIT_FACTOR);
            }
		}
	}//end GlobeNavigation
}//end namespace Navigation